package com.wangzhe.builder;

import com.wangzhe.bean.Constants;
import com.wangzhe.bean.FieldInfo;
import com.wangzhe.bean.TableInfo;
import com.wangzhe.utils.DateTimeUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王哲
 * @ClassName BuilderPojo
 * @create 2023--六月--下午4:38
 * @Description 构造查询实体类
 * @Version V1.0
 */
public class BuilderExample {

    private static Logger logger = LoggerFactory.getLogger(BuilderExample.class);

    public static void builderExample(TableInfo tableInfo) {
        File folder = new File(Constants.PACKAGE_EXAMPLE_PATH);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String beanName = tableInfo.getBeanName() + Constants.DB_SUFFIX_BEAN_PARAM;
        File file = new File(folder, beanName + ".java");

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        try {
            os = new FileOutputStream(file);
            osw = new OutputStreamWriter(os, "utf-8");
            bw = new BufferedWriter(osw);

            bw.write("package " + Constants.EXAMPLE_PATH + "; ");
            bw.newLine();
            bw.newLine();

            bw.write("import lombok.AllArgsConstructor;");
            bw.newLine();
            bw.write("import lombok.Data;");
            bw.newLine();
            bw.write("import lombok.NoArgsConstructor;");
            bw.newLine();
            bw.write("import lombok.ToString;");
            bw.newLine();


            if (tableInfo.isHaveBigDecimal()) {
                bw.write("import java.math.BigDecimal;");
                bw.newLine();
            }
            if (tableInfo.isHaveDate() || tableInfo.isHaveDateTime()) {
                bw.write("import java.util.Date;");
                bw.newLine();
            }

            bw.newLine();
            bw.newLine();
            /**
             * 构造类注释
             */
            BuilderComment.createClassComment(bw, beanName + "表的实体类");


            bw.write("@Data");
            bw.newLine();
            bw.write("@AllArgsConstructor");
            bw.newLine();
            bw.write("@NoArgsConstructor");
            bw.newLine();
            bw.write("@ToString");
            bw.newLine();
            bw.write("public class " + beanName + " extends BaseQuery {");
            bw.newLine();
            bw.newLine();


            //拿到所有属性
            for (FieldInfo fieldInfo : tableInfo.getFieldInfo()) {
                BuilderComment.createFieldComment(bw, fieldInfo.getComment());
                bw.write("\tprivate " + fieldInfo.getFieldJavaType() + " " + fieldInfo.getPropertyName() + ";");
                bw.newLine();
                bw.newLine();

                if (ArrayUtils.contains(Constants.SQL_STRING_TYPES, fieldInfo.getSqlType())) {
                    BuilderComment.createFieldComment(bw, fieldInfo.getComment() + "模糊匹配字段");
                    bw.write("\tprivate String " + fieldInfo.getPropertyName() + Constants.DB_SUFFIX_PARAM_LIKE + ";");
                    bw.newLine();
                    bw.newLine();
                }

                //如果是时间类型
                if (ArrayUtils.contains(Constants.SQL_DATE_TYPES, fieldInfo.getSqlType()) ||
                        ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPES, fieldInfo.getSqlType())) {

                    BuilderComment.createFieldComment(bw, fieldInfo.getComment() + "开始时间");
                    bw.write("\tprivate String " + fieldInfo.getPropertyName() + Constants.DB_SUFFIX_PARAM_START + ";");
                    bw.newLine();
                    bw.newLine();

                    BuilderComment.createFieldComment(bw, fieldInfo.getComment() + "结束时间");
                    bw.write("\tprivate String " + fieldInfo.getPropertyName() + Constants.DB_SUFFIX_PARAM_END + ";");
                    bw.newLine();
                    bw.newLine();
                }

            }
            bw.newLine();
            bw.write("}");
            bw.flush();
        } catch (Exception e) {
            logger.info("创建Example失败:{}", e.getMessage());
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
                if (osw != null) {
                    osw.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
